package com.swind.fast.modules.app.railway.report.service.impl;

import com.swind.fast.common.system.base.service.impl.SwindServiceImpl;
import com.swind.fast.modules.app.railway.project.entity.RailwayProjectTaskPoint;
import com.swind.fast.modules.app.railway.report.entity.RailwayReportTaskFinished;
import com.swind.fast.modules.app.railway.report.mapper.RailwayReportTaskFinishedMapper;
import com.swind.fast.modules.app.railway.report.service.IRailwayReportTaskFinishedService;
import com.swind.fast.modules.app.railway.project.service.IRailwayProjectTaskPointService;
import com.swind.fast.modules.app.railway.project.service.IRailwayProjectWorkmanService;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import com.swind.fast.modules.system.service.ISysDictService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.swind.fast.common.util.DateUtils;
import com.swind.fast.common.util.oConvertUtils;

import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
import java.util.Date;
import lombok.extern.slf4j.Slf4j;

/**
 * @Description: 项目
 */
@Slf4j
@Service
public class RailwayReportTaskFinishedServiceImpl extends SwindServiceImpl<RailwayReportTaskFinishedMapper, RailwayReportTaskFinished> implements IRailwayReportTaskFinishedService {

    @Autowired
    private ISysDictService sysDictService;
    @Autowired
    private IRailwayProjectTaskPointService railwayProjectTaskPointService;

    @Autowired
    private IRailwayProjectWorkmanService railwayProjectWorkmanService;

    public Map<String, Object> getWorkFinishedByProjectId(String projectId) {
        Map<String, Object> result = new HashMap<String, Object>();
        return result;
    }

    @Override
    public void generateTestFinshedPointReport(String pointId) {
        log.info("generateTestFinshedPointReport: {}", pointId);
        RailwayProjectTaskPoint point = railwayProjectTaskPointService.getById(pointId);

        Integer success = 1;

        try { generateReport(point);}
        catch (Exception e) {
            success = 2;
            e.printStackTrace();
        }

        point.setFinishReport(success);
        railwayProjectTaskPointService.updateById(point);
    }

    private void generateReport(RailwayProjectTaskPoint point) {
        String pointId = point.getId();
        String projectId = point.getProjectId();
        String siteName = point.getSiteName();
        String holeSerial = point.getHoleName() + " " + oConvertUtils.getString(point.getProbeId()) + "-" + oConvertUtils.getString(point.getDeviceName());

        String holeLocation = point.getPrefix() + calcHoleLocation(point.getMile()) + oConvertUtils.getString(point.getShift());

        // String holeLocation = point.getPrefix() + oConvertUtils.getString(point.getMile()) + oConvertUtils.getString(point.getShift());

        // String holeLocationP1 = String.valueOf(oConvertUtils.getInt(point.getMile()) / 1000);
        // String testMethod = (ProjectTestMethodsEnum.getByValue(point.getTestMethodDict()));

        String testMethod = sysDictService.queryDictTextByKey("railway_project_test_method", oConvertUtils.getString(point.getTestMethodDict()));
        String depthActual = point.getDeep();
        Date startDate = point.getTestDate();
        Date finishDate = startDate;
        Date materialDate = DateUtils.getDate();

        // 机长
        String operator = railwayProjectWorkmanService.getWorkmanByPosition(projectId, "4");
        // 项目负责人
        String technical = railwayProjectWorkmanService.getWorkmanByPosition(projectId, "1");

        log.info("generateTestFinshedPointReport: {}, name: {}", pointId, point.getHoleName());

        RailwayReportTaskFinished report = new RailwayReportTaskFinished();
        report.setProjectId(projectId);
        report.setPointId(pointId);
        report.setSiteName(siteName);
        report.setHoleSerial(point.getHoleName());
        report.setHoleLocation(holeLocation);
        report.setTestMethod(testMethod);
        report.setDepthDesign(depthActual);
        report.setDepthActual(depthActual);

        report.setStartDate(startDate);
        report.setFinishDate(startDate);
        report.setMaterialDate(materialDate);

        report.setOperator(operator);
        report.setTechnical(technical);

        this.save(report);
    }

    private String calcHoleLocation(String strMile) {
        Double dmile = oConvertUtils.getDouble(strMile, 0.00);
        String dstr = String.valueOf(dmile.intValue() / 1000);

        // Double dint = dmile / 1000;
        // String dstr = String.valueOf(dint.intValue());

        String pstr = String.format("%.2f", dmile % 1000);
        return dstr + "+" + pstr;
    }

}
